﻿using System;
using System.Collections.Generic;
using System.Text;


public abstract class Task
{
    public event TaskEventHandler WorkComplete;
    /// <summary>
    /// 异步调用挂接事件委托
    /// </summary>
    /// <param name="eventhandler">事件处理方法句柄</param>
    /// <param name="args">事件消息</param>
    protected void AsyncInvoke(TaskEventHandler eventhandler, TaskEventArgs args)
    {
        //TaskEventHandler[] tpcs = (TaskEventHandler[])eventhandler.GetInvocationList();
        Delegate[] tpcs = eventhandler.GetInvocationList();
        foreach (TaskEventHandler tpc in tpcs)
        {
            if (tpc.Target is System.Windows.Forms.Control)
            {
                System.Windows.Forms.Control targetForm = tpc.Target as System.Windows.Forms.Control;
                targetForm.BeginInvoke(tpc, new object[] { this, args });
            }
            else
            {
                tpc.BeginInvoke(this, args, null, null); //异步调用,启动后不管
            }
        }
    }

    /// <summary>
    /// 触发任务进度变化事件
    /// </summary>
    /// <param name="progress">任务进度(0-100)</param>
    /// <param name="result">任务工作中间结果对象</param>
    protected void FireProgressChangedEvent(string pn, string prog)
    {

        TaskEventArgs args = new TaskEventArgs(pn, prog);
        AsyncInvoke(WorkComplete, args);
    }

    public virtual void SearchbyPN(Object stateInfo)
    {
        return;
    }
}


/// <summary>
/// 任务状态消息
/// </summary>
public class TaskEventArgs : EventArgs
{
    /// <summary>
    /// 任务运行结果
    /// </summary>
    public string PN;
    /// <summary>
    /// 任务进度(0-100)
    /// </summary>
    public string Progress;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="pn"></param>
    /// <param name="progress"></param>
    public TaskEventArgs(string pn, string progress)
    {
        this.PN = pn;
        this.Progress = progress;
    }
}